<!DOCTYPE html>
<html>  
  <head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="x-nav">
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input type="text" name="username"  placeholder="请输入名称" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock>
          <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
          <button class="layui-btn" onclick="x_admin_show('添加','add_drinkadjust.html',600,500)"><i class="layui-icon"></i>添加</button>
      </xblock>
      <table id="catelist" lay-filter="catelist">
      </table>
    </div>
    <script>
      layui.use(['table','layer'], function(){
        $ = layui.jquery;
        var table = layui.table,
        layer = layui.layer;
        //表格数据填充
        table.render({
          elem: '#catelist',
          // height: 315,
          url: '/admin/drinkadjust', //数据接口
          page: true, //开启分页
          loading:true,//加载显示
          text:{ none: '暂无相关数据'},
          done:()=>{layer.msg('加载完成！',{time:600});},
          cols: [[ //表头
            {checkbox: true},
            {field: 'drink_ratio_id', title: '序号',type:'numbers'},
            {field: 'cate_name', title: '饮料类型'},
            {field: 'drink_ratio_num', title: '饮料规格',templet:'<div>{{d.drink_ratio_num}}ml</div>'},
            {field: 'drink_radio_desc', title: '饮料词条建议'},
            {field: 'drink_ratio_comments', title: '词条备注'},
            {title: '操作',width:120,templet: '#titleTpl'},
          ]]
        });
        //监听工具条
        table.on('tool(catelist)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
          var data = obj.data; //获得当前行数据
          var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
          var tr = obj.tr; //获得当前行 tr 的DOM对象
          if(layEvent === 'del'){ //删除
            layer.confirm('真的删除行么', function(index){
              //向服务端发送删除指令
              $.get('/admin/delcate?id='+data.id,(data,status)=>{
                if(status=='success'&&data==1){
                  obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                  layer.close(index);
                }else{
                  layer.msg('删除失败',{time:600});
                }
              });
            });
          } else if(layEvent === 'edit'){ //编辑
            //数据放入本地session
            window.sessionStorage.setItem('cate-item',JSON.stringify(data));
            //打开弹窗
            x_admin_show('编辑','edit_drinkadjust.html',600,500);
            //同步更新缓存对应的值
            obj.update(data);
          }
        });
        //========================================
      });
    </script>

    <script type="text/html" id="titleTpl">
      <button lay-event="edit" class="layui-btn layui-btn-danger layui-btn-sm"><i class="iconfont">&#xe69e;</i></button>
      <button lay-event="del" class="layui-btn layui-btn-danger layui-btn-sm"><i class="iconfont">&#xe69d;</i></button>
    </script>
  </body>

</html>